 function [pdf,logpdf]=mnormalpdf(x,mu,sig,normconst); 
% function [pdf,logpdf]=mnormalpdf(x,mu,sig,normconst); 
% Computes the pdf and logpdf of the multivariate normal (mu, sig)
% for each column of the matrix X 
% x = nump x T    mu =nump x 1 
% sig = nump x nump is the VCV; NOT the CHOL
% Can additionally define a normalizing constant -other than (2pi)^(-nump/2) 
% such that logpdfrs(x)=logpdf(x) - log(normconst) 
% If no fourth argument constant not defined 
% Output 1 x T vector of pdfs and logpdfs
% Modified 4/14/03
[n1,it]=size(x); 
if  nargin == 2;
    error('Missing Arguments to compute Normal');
end;
if nargin >= 3 ; 
    n2=size(mu,1); 
    n3=(size(sig))';
    m1=[n1;n1]; 
    if (n1 ~= n2) | (m1 ~= n3); 
        error('Wrong Dimension in Inputs')
    end; 
end; 
if nargin==1; 
    mu=zeros(n1,1); 
    sig=zeros(n1); 
end;
pdf=zeros(it,1); 
logpdf=zeros(it,1); 
ii=1; 
while ii<=it;     
    dmn=(x(:,ii)-mu);
    insig=eye(n1)/sig; 
    ft=0.5*(dmn')*insig*dmn; 
    c=(n1/2)*log(2*pi); 
    de=0.5*log(det(sig)); 
    if nargin <= 3; 
        logpdf(ii,1)=-(c+de+ft); 
    else; 
        logpdf(ii,1)=-(c+de+ft+(log(normconst)));
    end; 
    pdf(ii,1)=exp(logpdf(ii,1));     
    ii=ii+1; 
end; 